//
//  AuthenCompanyMercMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import SwiftUI

class AuthenCompanyMercMsgVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*10
    
    var merchantType = "0"
    
    var controller = WHBaseViewVC()
    var mccMsgDict = NSDictionary()
    var mccWechatDict = NSDictionary()
    var mccAlipayDict = NSDictionary()
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var photoTitleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "商户信息"
        return vm
    }()
    lazy var mercNameAllVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.photoTitleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "商户全称"
        vm.textField.placeholder = "请输入商户全称"
        vm.textField.keyboardType = .default
        vm.maxLength = 200
        
        return vm
    }()
    lazy var mercNameSimpleVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercNameAllVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "商户简称"
        vm.textField.placeholder = "请输入商户简称"
        vm.textField.keyboardType = .default
        return vm
    }()
    lazy var mercMoneyVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercNameSimpleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "注册资金（万元）"
        vm.textField.placeholder = "请输入注册资金"
        vm.textField.keyboardType = .decimalPad
        return vm
    }()
    lazy var mercCodeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercMoneyVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "统一信用代码"
        vm.textField.placeholder = "请输入统一信用代码"
//        vm.textField.keyboardType = .de
        return vm
    }()
    
    lazy var mercTimeStartVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercCodeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "经营期起始"
        vm.textField.placeholder = "请选择经营期起始日期"
        vm.showArrowImg()
        return vm
    }()
    
    lazy var mercTimeEndVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercTimeStartVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "经营期限至"
        vm.textField.placeholder = "无时间请选择长期"
        vm.showArrowImg()
        return vm
    }()
    lazy var mccVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mercTimeEndVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "行业分类"
        vm.textField.placeholder = "请选择一级分类"
        vm.showArrowImg()
        
        vm.tapBlock = {()in
            let vc = AuthenMccListVC()
            vc.mccType = "mcc"
            vc.posChannel = AuthenCompanyMsgModel.shareInstance.posChannel
            vc.merchantType = "\(self.merchantType)"
            vc.tapBlock = {(dict)in
                self.mccMsgDict = dict
                self.mccVm.setLabelContent(content: self.mccMsgDict["mccName"]as? String ?? "")
                AuthenCompanyMsgModel.shareInstance.mercMccCode = self.mccMsgDict["mcc"]as? String ?? ""
                AuthenCompanyMsgModel.shareInstance.mercMccName = self.mccMsgDict["mccName"]as? String ?? ""
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var mccAlipayVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mccVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "支付宝行业"
        vm.textField.placeholder = "请选择支付宝行业"
        vm.showArrowImg()
        vm.isHidden = true
        vm.tapBlock = {()in
            let vc = AuthenMccListVC()
            vc.mccType = "alipay"
            vc.posChannel = AuthenCompanyMsgModel.shareInstance.posChannel
            vc.merchantType = "\(self.merchantType)"
            vc.tapBlock = {(dict)in
                self.mccAlipayDict = dict
                self.mccAlipayVm.setLabelContent(content: self.mccAlipayDict["typeThree"]as? String ?? "")
                AuthenCompanyMsgModel.shareInstance.mercAlipayMccCode =
                "\(self.mccAlipayDict["mccCode"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.mercAlipayMccName =
                self.mccAlipayDict["typeThree"]as? String ?? ""
                AuthenCompanyMsgModel.shareInstance.mercAlipayMcc =
                "\(AuthenCompanyMsgModel.shareInstance.mercAlipayMccName)_\(AuthenCompanyMsgModel.shareInstance.mercAlipayMccCode)"
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var mccWeChatVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mccVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "微信行业"
        vm.textField.placeholder = "请选择微信行业"
        vm.showArrowImg()
        vm.isHidden = true
        vm.tapBlock = {()in
            let vc = AuthenMccListVC()
            vc.mccType = "wechat"
            vc.posChannel = AuthenCompanyMsgModel.shareInstance.posChannel
            vc.merchantType = "\(self.merchantType)"
            vc.tapBlock = {(dict)in
                self.mccWechatDict = dict
                self.mccWeChatVm.setLabelContent(content: self.mccWechatDict["typeThree"]as? String ?? "")
                AuthenCompanyMsgModel.shareInstance.mercWeChatMccCode = "\(self.mccWechatDict["mccId"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.mercWeChatMccName = "\(self.mccWechatDict["typeThree"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.mercWeChatMcc = "\(AuthenCompanyMsgModel.shareInstance.mercWeChatMccName)_\(AuthenCompanyMsgModel.shareInstance.mercWeChatMccCode)"
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var areaCodeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.mccVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "地区码"
        vm.textField.placeholder = "请选择地区码"
        vm.showArrowImg()
        return vm
    }()
//    lazy var mercJingYingAreaVm : RegisterInputVM = {
//        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.areaCodeVm.frame.maxY, width: 0, height: 0))
//        vm.leftLabel.text = "经营范围"
//        vm.textField.placeholder = "请输入经营范围"
//        vm.textField.keyboardType = .default
//        vm.maxLength = 500
//        return vm
//    }()
    lazy var addressDetailVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.areaCodeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "营业执照详细地址"
        vm.textField.placeholder = "请输入营业执照详细地址"
        vm.textField.keyboardType = .default
        vm.maxLength = 100
        return vm
    }()
    lazy var emailVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.addressDetailVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "电子邮箱"
        vm.textField.placeholder = "请输入电子邮箱"
        vm.isHidden = true
        vm.maxLength = 50
        return vm
    }()
//    lazy var mercJingYingAreaVm : AuthenRemarkVM = {
//        let vm = AuthenRemarkVM.init(frame: CGRect.init(x: 0, y: self.addressDetailVm.frame.maxY, width: 0, height: 0))
//        vm.titleLabel.text = "经营范围"
//        vm.placeHolderLabel.text = "请输入经营范围"
//        return vm
//    }()
    lazy var mercJingYingAreaVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.addressDetailVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "经营范围"
        vm.textField.placeholder = "请选择经营范围"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.sendBusinessScodeRequest()
        }
        return vm
    }()
    lazy var remarkVm : AuthenRemarkVM = {
        let vm = AuthenRemarkVM.init(frame: CGRect.init(x: 0, y: self.mercJingYingAreaVm.frame.maxY, width: 0, height: 0))
        vm.isHidden = true
        return vm
    }()
}

extension AuthenCompanyMercMsgVM{
    func initUI() {
        
        addSubview(photoTitleVm)
        addSubview(mercNameAllVm)
        addSubview(mercNameSimpleVm)
        addSubview(mercMoneyVm)
        addSubview(mercCodeVm)
        addSubview(mercTimeStartVm)
        addSubview(mercTimeEndVm)
        addSubview(mccVm)
        addSubview(mccAlipayVm)
        addSubview(mccWeChatVm)
        addSubview(areaCodeVm)
        addSubview(mercJingYingAreaVm)
        addSubview(addressDetailVm)
        addSubview(emailVm)
        addSubview(remarkVm)
        
        if UserModel.shared.posChannel == "fy"{
            self.updateUIShowWeChatMcc()
        }
    }
    func updateUIShowWeChatMcc() {
        selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*11
        
        let selfFrame = self.frame
        self.frame = CGRect.init(x: selfFrame.origin.x, y: selfFrame.origin.y, width: selfFrame.size.width, height: selfHeight)
        
        mccWeChatVm.isHidden = false
        let areaCodeCenter = areaCodeVm.center
        
        areaCodeVm.center = CGPoint.init(x: areaCodeCenter.x, y: areaCodeCenter.y + areaCodeVm.selfHeight)
        mercJingYingAreaVm.center = CGPoint.init(x: areaCodeCenter.x, y: mercJingYingAreaVm.center.y + areaCodeVm.selfHeight)
        addressDetailVm.center = CGPoint.init(x: areaCodeCenter.x, y: addressDetailVm.center.y + areaCodeVm.selfHeight)
        emailVm.center = CGPoint.init(x: areaCodeCenter.x, y: emailVm.center.y + areaCodeVm.selfHeight)
        remarkVm.center = CGPoint.init(x: areaCodeCenter.x, y: remarkVm.center.y + areaCodeVm.selfHeight)
    }
    func updateLicenceMsg(dict:NSDictionary) {
        mercNameAllVm.textField.text = "\(dict["merchantName"]as? String ?? "")"
        mercTimeStartVm.setLabelContent(content: "\(dict["licenseStart"]as? String ?? "")")
        mercTimeEndVm.setLabelContent(content: "\(dict["licenseEnd"]as? String ?? "")")
        addressDetailVm.textField.text = "\(dict["merchantAddress"]as? String ?? "")"
        mercCodeVm.textField.text = "\(dict["licenseNo"]as? String ?? "")"
        mercJingYingAreaVm.setLabelContent(content: "\(dict["businessScope"]as? String ?? "")")
//        mercJingYingAreaVm.textView.text = "\(dict["businessScope"]as? String ?? "")"
        
        AuthenCompanyMsgModel.shareInstance.licenseStartTime = "\(dict["licenseStart"]as? String ?? "")"
        AuthenCompanyMsgModel.shareInstance.licenseEndTime = "\(dict["licenseEnd"]as? String ?? "")"
        
    }
    //MARK: 回显上次填写的信息
    func updateUIForRefuse() {
        mercNameAllVm.textField.text = AuthenCompanyMsgModel.shareInstance.mchName
        mercNameSimpleVm.textField.text = AuthenCompanyMsgModel.shareInstance.mchNameSimple
        mercMoneyVm.textField.text = AuthenCompanyMsgModel.shareInstance.money
        mercCodeVm.textField.text = AuthenCompanyMsgModel.shareInstance.licenseNo
        mercTimeStartVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.licenseStartTime)
        mercTimeEndVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.licenseEndTime)
        mccVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.mercMccName)
        mccWeChatVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.mercWeChatMccName)
        areaCodeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.addressProvinceName)\(AuthenCompanyMsgModel.shareInstance.addressCityName)\(AuthenCompanyMsgModel.shareInstance.addressAreaName)")
        addressDetailVm.textField.text = AuthenCompanyMsgModel.shareInstance.licenseAddress
        emailVm.textField.text = AuthenCompanyMsgModel.shareInstance.email
        
        if AuthenCompanyMsgModel.shareInstance.licenseArea.count > 0 {
//            mercJingYingAreaVm.textView.text = AuthenCompanyMsgModel.shareInstance.licenseArea
            mercJingYingAreaVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.licenseArea)
        }
        if AuthenCompanyMsgModel.shareInstance.remark.count > 0{
            remarkVm.textView.text = AuthenCompanyMsgModel.shareInstance.remark
        }
    }
    //个体进件，隐藏注册资金
    func updateUIforGeTi() {
        mercJingYingAreaVm.frame = addressDetailVm.frame
        addressDetailVm.frame = areaCodeVm.frame
        areaCodeVm.frame = mccVm.frame
        mccVm.frame = mercTimeEndVm.frame
        mercTimeEndVm.frame = mercTimeStartVm.frame
        mercTimeStartVm.frame = mercCodeVm.frame
        mercCodeVm.frame = mercMoneyVm.frame
        mercMoneyVm.isHidden = true
    }
}

extension AuthenCompanyMercMsgVM{
    func getValue() {
        AuthenCompanyMsgModel.shareInstance.mchName = mercNameAllVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.mchNameSimple = mercNameSimpleVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.money = mercMoneyVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.licenseNo = mercCodeVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.licenseStartTime = mercTimeStartVm.rightLabel.text ?? ""
        AuthenCompanyMsgModel.shareInstance.licenseEndTime = mercTimeEndVm.rightLabel.text ?? ""
//        AuthenCompanyMsgModel.shareInstance.licenseArea = mercJingYingAreaVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.licenseArea = mercJingYingAreaVm.rightLabel.text ?? ""
//        AuthenCompanyMsgModel.shareInstance.licenseArea = mercJingYingAreaVm.textView.text ?? ""
        
        AuthenCompanyMsgModel.shareInstance.licenseAddress = addressDetailVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.email = emailVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.remark = remarkVm.textView.text ?? ""
    }
}

extension AuthenCompanyMercMsgVM{
    func sendBusinessScodeRequest() {
        WHNetworkUtil.shareManager().POST(urlString: URL_getBusinessScopeList, parameters: nil) { responseObject in
            DLLog(message: "\(responseObject)")
            
            let dataArray = responseObject["data"]as? NSArray ?? []
            let alert = UIAlertController(title: "请选择经营范围", message: nil, preferredStyle: .actionSheet)
            let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler: nil)
            
            for i in 0..<dataArray.count{
                let itemAction = UIAlertAction(title:"\(dataArray[i]as? String ?? "")", style: .default){ action in
                    self.mercJingYingAreaVm.setLabelContent(content: "\(dataArray[i]as? String ?? "")")
                }
                alert.addAction(itemAction)
            }
            
            alert.addAction(cancelAction)
            self.controller.present(alert, animated: true)
        }
    }
}

